package yxy.game.pm2.bean.db.dao;

import com.alibaba.fastjson.JSONObject;
import org.hibernate.Session;
import org.hibernate.query.Query;
import yxy.apple.database.BaseDAO;
import yxy.apple.database.DAOAccountManager;
import yxy.apple.database.DAOGameManager;
import yxy.apple.database.DAOManager;
import yxy.game.pm2.bean.db.module.cache;

import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class MySQLCacheDAO extends BaseDAO<cache> {

    private static String getPrefix(String key) {
        //1、如果存在这种格式的key：PLAYER:0vu4nw:MODULE:HEROINFO，则取出pcode的首位做分表
        //2、如果不是第1种格式，则统一取key的首位做分表
        return (key.indexOf("PLAYER:") == 0)
                ? key.split(":")[1].substring(0, 1).toLowerCase()
                : key.substring(0, 1).toLowerCase();
    }

    //表名
    private static String getTableName(String key) {
        return "cache_" + getPrefix(key);
    }

    public static cache get(String key) throws Exception {
        Map<String, Object> params = new Hashtable<>();
        params.put("key", key);

        String sql = "select * from " + getTableName(key) + " where `key` = :key";
        System.out.println(sql + "," + key);
        List<cache> list = queryBySql(DAOGameManager.getGameH(), cache.class, sql, params);
        return (list != null && list.size() > 0) ? list.get(0) : null;
    }

    public static boolean add(cache db) {
        String sql = "insert into " + getTableName(db.getKey()) + " (`key`, `value`, `createtime`, `updatetime`) values (:key,:value,:createtime,:updatetime)";
        Map<String, Object> params = new Hashtable<>();
        params.put("key", db.getKey());
        params.put("value", db.getValue());
        params.put("createtime", db.getCreatetime());
        params.put("updatetime", db.getUpdatetime());
        int result = MySQLCacheDAO.execBySql(DAOGameManager.getGameH(), sql, params);
        return result > 0;
    }

    public static boolean update(cache db) {
        String sql = "update " + getTableName(db.getKey()) + " set `key`=:key,`value`=:value,`createtime`=:createtime,`updatetime`=:updatetime where `key`=:key";
        Map<String, Object> params = new Hashtable<>();
        params.put("key", db.getKey());
        params.put("value", db.getValue());
        params.put("createtime", db.getCreatetime());
        params.put("updatetime", db.getUpdatetime());

        int result = MySQLCacheDAO.execBySql(DAOGameManager.getGameH(), sql, params);
        return result > 0;
    }


    //删
    public static boolean delete(String key) {
        String sql = "delete from " + getTableName(key) + " where `key`=:key ";
        Map<String, Object> params = new Hashtable<>();
        params.put("key", key);
        int result = MySQLCacheDAO.execBySql(DAOGameManager.getGameH(), sql, params);
        return result > 0;
    }


    //object -> json 存储
    public static void setV(String key, Object object) throws Exception {
        String value = JSONObject.toJSONString(object);
        cache che = MySQLCacheDAO.get(key);
        Date now = new Date();
        //数据库表里面存在当前数据就覆盖or新增
        if (che != null) {
            che.setValue(value);
            che.setUpdatetime(now);
            MySQLCacheDAO.update(che);
        } else {
            che = new cache();
            che.setKey(key);
            che.setValue(value);
            che.setCreatetime(now);
            che.setUpdatetime(now);
            MySQLCacheDAO.add(che);
        }
    }

    //json -> object 取出
    public static <T> T getV(String key, Class<T> obj) throws Exception {
        cache che = MySQLCacheDAO.get(key);
        if (che == null) return null;

        return (T) JSONObject.parseObject(che.getValue(), obj);
    }

    public static void main(String[] args) {
        String key = "PLAYER:v0u4nw:MODULE:HEROINFO";
        System.out.println(key.indexOf("PLAYER:"));
        System.out.println(key.split(":")[1].substring(0, 1));
    }
}
